package com.project.common.controller;

import com.alibaba.fastjson.JSONObject;
import com.project.common.entity.SysMenuRole;
import com.project.common.service.SysMenuRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/sysMenuRole")
@RestController
public class SysMenuRoleController {
    @Autowired
    private SysMenuRoleService sysMenuRoleService;
    @PostMapping("/grantAndRevokeMenuFromRole.action")
    public int grantAndRevokeMenuFromRole(String menuIdsStr,Integer roleId,String menuIdsNoCheck){
        Integer[] menuIds = JSONObject.parseObject(menuIdsStr, Integer[].class);
        Integer[] menuNoCheckIds = JSONObject.parseObject(menuIdsNoCheck, Integer[].class);
        //删除角色对应的所有菜单
        if(menuNoCheckIds!=null){

            for(int i=0;i<menuNoCheckIds.length;i++){
                int i1 = sysMenuRoleService.deleteByRoleIdAndMenuId(roleId, menuNoCheckIds[i]);
            }
        }
        //添加勾选的菜单
        if(menuIds!=null){

            for(int i=0;i<menuIds.length;i++){
                List<SysMenuRole> sysMenuRoles = sysMenuRoleService.selectByRoleIdAndMenuId(roleId, menuIds[i]);
                if(sysMenuRoles.size()<=0){
                    SysMenuRole sysMenuRole = new SysMenuRole();
                    sysMenuRole.setMenuId(menuIds[i]);
                    sysMenuRole.setRoleId(roleId);
                    sysMenuRoleService.insertSelective(sysMenuRole);
                }

            }
        }
        return 0;
    }
}
